package com.mycompany.integration;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

/**
 * Clase con Test de Integracion para testear el buen funcionamiento de operaciones POST PUT DELETE del Web Service.
 * Es necesario que la aplicacion este desplegada en el siguiente contexto:
 * WADL: http://localhost:8080/ACLREST/services/api?_wadl
 * @author javachavez
 */
public class NetClientPostPutDeleteIT {
	protected final Log log = LogFactory.getLog(getClass());
	@Test
    public void testCreate() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/create";
			
			URL url = new URL(strUrl );
			
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");

			String input ="{\"origen\":\"192.168.0.10\","
					+ "\"destino\":\"192.168.0.22\","
					+ "\"protocolo\":\"TCP\","
					+ "\"accion\":\"ALLOW\"}";
					
			OutputStream os = conn.getOutputStream();
			os.write(input.getBytes());
			os.flush();

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}
	
	@Test(expected=Exception.class)
    public void testWrongProtocoloCreate() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/create";
			
			URL url = new URL(strUrl );
			
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");

			String input ="{\"origen\":\"192.168.0.10\","
					+ "\"destino\":\"192.168.0.22\"},"
					+ "\"protocolo\":\"TCI\","
					+ "\"accion\":\"ALLOW\"}";
					
			OutputStream os = conn.getOutputStream();
			os.write(input.getBytes());
			os.flush();

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}
	
	@Test(expected=Exception.class)
    public void testWrongAccionCreate() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/create";
			
			URL url = new URL(strUrl );
			
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");

			String input ="{\"origen\":\"192.168.0.10\","
					+ "\"destino\":\"192.168.0.22\"},"
					+ "\"protocolo\":\"TCP\","
					+ "\"accion\":\"AL\"}";
					
			OutputStream os = conn.getOutputStream();
			os.write(input.getBytes());
			os.flush();

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}

	@Test
    public void testUpdate() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/update";
			
			URL url = new URL(strUrl );
			
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("PUT");
			conn.setRequestProperty("Content-Type", "application/json");

			String input ="{\"id\":\"1\",\"origen\":\"192.168.0.10\","
					+ "\"destino\":\"192.168.0.22\","
					+ "\"protocolo\":\"TCP\","
					+ "\"accion\":\"ALLOW\"}";
					
			OutputStream os = conn.getOutputStream();
			os.write(input.getBytes());
			os.flush();

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}
	
	@Test(expected=Exception.class)
    public void testWrongAccionUpdate() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/update";
			
			URL url = new URL(strUrl );
			
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("PUT");
			conn.setRequestProperty("Content-Type", "application/json");

			String input ="{\"id\":\"1\",\"origen\":\"192.168.0.10\","
					+ "\"destino\":\"192.168.0.22\","
					+ "\"protocolo\":\"TCP\","
					+ "\"accion\":\"ALLO\"}";
					
			OutputStream os = conn.getOutputStream();
			os.write(input.getBytes());
			os.flush();

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}
	
	@Test(expected=Exception.class)
    public void testWrongProtocoloUpdate() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/update";
			
			URL url = new URL(strUrl );
			
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("PUT");
			conn.setRequestProperty("Content-Type", "application/json");

			String input ="{\"id\":\"1\",\"origen\":\"192.168.0.10\","
					+ "\"destino\":\"192.168.0.22\","
					+ "\"accion\":\"ALLOW\"}";
					
			OutputStream os = conn.getOutputStream();
			os.write(input.getBytes());
			os.flush();

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}
	
	@Test
    public void testRemove() throws Exception {

			String strUrl="http://localhost:8080/ACLREST/services/api/controlacceso/remove/2";
			
			URL url = new URL(strUrl );
			
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("DELETE");

			if (conn.getResponseCode() !=200 && conn.getResponseCode()!= HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			log.info("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				log.info(output);
			}

			conn.disconnect();
	}

}